﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RuyeeModbus
{
    public class ModbusTCPTransactionContorl
    {
        static List<ModbusTCPTransaction> TransactionList = new List<ModbusTCPTransaction>();
        private ModbusTCPTransactionContorl()
        {

        }
        private static ModbusTCPTransactionContorl instance;
        public static ModbusTCPTransactionContorl Instance
        {
            get
            {
                if (instance == null)// || TransactionList == null)
                {
                    instance = new ModbusTCPTransactionContorl();
                    //TransactionList = new List<ModbusTCPTransaction>();
                }
                return ModbusTCPTransactionContorl.instance;
            }
        }

        public void AddTransaction(ModbusTCPTransaction Transaction)
        {
            TransactionList.Add(Transaction);
        }

        public ModbusTCPTransaction GetTransactionByName(string name)
        {
            return new ModbusTCPTransaction();
        }


        public ModbusTCPTransaction GetTransactionByTransactionId(int TransactionID)
        {

            byte TransactionIDFrist = 0;
            byte TransactionIDSecond = 0;

            if (TransactionID > 255)
            {
                TransactionIDFrist = (byte)(TransactionID >> 8);
                TransactionIDSecond = (byte)(TransactionID << 8 >> 8);
            }
            else
                TransactionIDSecond = (byte)(TransactionID);

            ModbusTCPTransaction T = new ModbusTCPTransaction();
            for (int i = 0; i < TransactionList.Count; i++)
            {

                if (TransactionList[i] == null)
                {
                    lock (TransactionList)
                    {
                        TransactionList.RemoveAt(i);
                    }
                }

                if (TransactionList[i].RequestADU.MBAP.TransactionIDFrist == TransactionIDFrist
                    && TransactionList[i].RequestADU.MBAP.TransactionIDSecond == TransactionIDSecond)
                {
                    T = TransactionList[i];
                }
            }

            return T;
        }

        public void SetResponseADU(ModbusTCP_ADU adu)
        {


            for (int i = 0; i < TransactionList.Count; i++)
            {
                if (TransactionList[i] == null)
                {
                    TransactionList.RemoveAt(i);
                }
                if (TransactionList[i].RequestADU.MBAP.TransactionIDFrist == adu.MBAP.TransactionIDFrist
                    && TransactionList[i].RequestADU.MBAP.TransactionIDSecond == adu.MBAP.TransactionIDSecond
                    && TransactionList[i].RequestADU.MBAP.NetID == adu.MBAP.NetID
                    )
                {
                    TransactionList[i].ResponseADU = adu;
                }
            }

        }

        internal void RemoveTransaction(int TransactionID)
        {

            byte TransactionIDFrist = 0;
            byte TransactionIDSecond = 0;

            if (TransactionID > 255)
            {
                TransactionIDFrist = (byte)(TransactionID >> 8);
                TransactionIDSecond = (byte)(TransactionID << 8 >> 8);
            }
            else
                TransactionIDSecond = (byte)(TransactionID);

            for (int i = 0; i < TransactionList.Count; i++)
            {
                if (TransactionList[i] == null)
                {
                    TransactionList.RemoveAt(i);
                }
                if (TransactionList[i].RequestADU.MBAP.TransactionIDFrist == TransactionIDFrist
                    && TransactionList[i].RequestADU.MBAP.TransactionIDSecond == TransactionIDSecond)
                {
                    TransactionList.RemoveAt(i);
                }
            }

        }

        internal void ReInit()
        {
            //lock (TransactionList)
            //{
            TransactionList = new List<ModbusTCPTransaction>();
            //}
        }
    }
}
